//
//  ZYCTeaLeaveAddPhotoCell.m
//  ZDSC
//
//  Created by 张威 on 2021/10/20.
//

#import "ZYCTeaLeaveAddPhotoCell.h"
#import "ZYCAddPhotoPlaceHolderCell.h"
#import <UIImageView+WebCache.h>

#define BTImageTotalNumber 9
@interface ZYCTeaLeaveAddPhotoCell ()<UICollectionViewDelegate,UICollectionViewDataSource,ZYCPhotoCollectionCellDelegate>
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIView *line;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;


@end
@implementation ZYCTeaLeaveAddPhotoCell

- (void)setDataSource:(NSMutableArray *)dataSource{
    _dataSource = dataSource;
    [self.collectionView reloadData];
}
- (void)setSelectedPhotos:(NSMutableArray *)selectedPhotos{
    _selectedPhotos = selectedPhotos;
    [self.collectionView reloadData];
}

- (void)setTitleString:(NSString *)titleString{
    _titleString = titleString;
    _titleLabel.text = titleString;
}

+ (instancetype)teacherLeaveAddPhotoCellWithType:(ZYCTeaLeaveAddPhotoType)type TableView:(UITableView *)tableView{
    static NSString *rid = @"kReuseIdentifier";
    ZYCTeaLeaveAddPhotoCell *cell = [tableView dequeueReusableCellWithIdentifier:rid];
    if (!cell) {
        cell = [[ZYCTeaLeaveAddPhotoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:rid];
        cell.type = type;
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.line];
        [self.contentView addSubview:self.collectionView];

        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(15.0f);
            make.top.equalTo(self.contentView).offset(20.0f);
        }];
        
        [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.contentView);
            make.top.equalTo(self.titleLabel.mas_bottom).offset(20.0f);
            make.height.mas_equalTo(1.0f);
        }];
        
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.line.mas_bottom).offset(10.0f);
            make.left.equalTo(self.contentView).offset(15.0f);
            make.right.equalTo(self.contentView).offset(-15.0f);
            make.bottom.equalTo(self.contentView).offset(-15.0f);
            make.height.mas_equalTo(65.0f);
        }];
        
        
    }
    return self;
}

#pragma mark - UICollectionViewDataSource && UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (self.type == ZYCTeaLeaveAddPhotoTypeShow) {
        return self.dataSource.count;
    }else{
        if (self.dataSource.count == BTImageTotalNumber) {
            return self.dataSource.count;
        }else{
            return self.dataSource.count + 1;
        }
    }
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.type == ZYCTeaLeaveAddPhotoTypeShow) {
        ZYCPhotoCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ZYCPhotoCollectionCell class]) forIndexPath:indexPath];
        cell.type = self.type;
        ZYCTeaLeaveRecordFileModel *model = self.dataSource[indexPath.item];
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/zd-system-service%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"sdfz_url"],model.filePath]];
        [cell.iconImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"wushuju"]];
        cell.delegate = self;
        return cell;
    }else{
        if (indexPath.item == self.dataSource.count) {
            ZYCAddPhotoPlaceHolderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ZYCAddPhotoPlaceHolderCell class]) forIndexPath:indexPath];
            return cell;
        }else{
            ZYCPhotoCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ZYCPhotoCollectionCell class]) forIndexPath:indexPath];
            cell.delegate = self;
            cell.type = self.type;
            cell.iconImageView.image = self.dataSource[indexPath.item];
            return cell;
        }
    }
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.type == ZYCTeaLeaveAddPhotoTypeAdd) {
        if (indexPath.item == self.dataSource.count) {
            if ([self.delegate respondsToSelector:@selector(teaLeaveAddPhoto)]) {
                [self.delegate teaLeaveAddPhoto];
            }
        }
    }else{
        if ([self.delegate respondsToSelector:@selector(teaLeaveBrowserWithIndex:PhotoCell:)]) {
            [self.delegate teaLeaveBrowserWithIndex:indexPath.item PhotoCell:self];
        }        
    }
}


#pragma mark - ZYCPhotoCollectionCellDelegate
- (void)photoCollectionCellDidClickDelete:(ZYCPhotoCollectionCell *)cell{
    
    [self.dataSource removeObject:cell.iconImageView.image];
    [self.collectionView reloadData];
    
    if ([self.delegate respondsToSelector:@selector(teaLeaveDeletePhotoCell:)]) {
        [self.delegate teaLeaveDeletePhotoCell:self];
    }
    
    
}


#pragma mark - getters


- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel LabelWithFont:kCustomFont(@"PingFangSC-Medium", 16.0f) titleColor:HEX_RGB(@"#333333") textAlignment:ZZTextAlignmentLeft];
    }
    return _titleLabel;
}

- (UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = HEX_RGB(@"#FAFAFA");
    }
    return _line;
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        [_collectionView registerClass:[ZYCAddPhotoPlaceHolderCell class] forCellWithReuseIdentifier:NSStringFromClass([ZYCAddPhotoPlaceHolderCell class])];
        [_collectionView registerClass:[ZYCPhotoCollectionCell class] forCellWithReuseIdentifier:NSStringFromClass([ZYCPhotoCollectionCell class])];
    }
    return _collectionView;
}

- (UICollectionViewFlowLayout *)layout{
    if (!_layout) {
        _layout = [[UICollectionViewFlowLayout alloc] init];
        _layout.itemSize = CGSizeMake(63.0f, 63.0f);
        _layout.minimumLineSpacing = 0;//行间距
        _layout.minimumInteritemSpacing = 0;//列间距
        _layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    return _layout;
}




@end
